import argparse
from crawler import Crawler

# example:
# python main.py --url="http://www.sanjia2.cn" --mobiletype="2" --output="../sitemap.xml"
# python main.py --url="http://hkcms.temp0.com" --mobiletype="2" --output="../sitemap.xml"
# python main.py --url="http://www.samjia.cn" --mobiletype="3" --output="../sitemap.xml"
# 以下为排除带?的链接
# python main.py --url="http://hkcms.temp0.com" --mobiletype="2" --output="../sitemap.xml" --exclude="\?"

# initializing parameters
parser = argparse.ArgumentParser(description="Sitemap generator")
parser.add_argument('--url', action="store", default="", help="For example https://www.finstead.com")
parser.add_argument('--exclude', action="store", default="", help="regex pattern to exclude. For example 'symbol/info' will exclude https://www.finstead.com/symbol/info/ORCL")
parser.add_argument('--no-verbose', action="store_true", default="", help="print verbose output")
parser.add_argument('--output', action="store", default="sitemap.xml", help="File path for output, if file exists it will be overwritten")
parser.add_argument('--mobiletype', action="store", default="", help="For example 1:mobile(移动网页),2:pc,mobile(自适应网页),3:htmladapt(代码适配)")

# parsing parameters
args = parser.parse_args()
url = args.url.rstrip("/")

mobiletypeval = ''
if args.mobiletype == '1':
	mobiletypeval = '\n\t\t<mobile:mobile type="mobile"/>\n\t'
if args.mobiletype == '2':
	mobiletypeval = '\n\t\t<mobile:mobile type="pc,mobile"/>\n\t'
if args.mobiletype == '3':
	mobiletypeval = '\n\t\t<mobile:mobile type="htmladapt"/>\n\t'

found_links = []

# initializeing crawler
crawler = Crawler(url, exclude=args.exclude, no_verbose=args.no_verbose);

# fetch links
links = crawler.start()


#write into file
with open(args.output, "w") as file: 
	file.write('<?xml version="1.0" encoding="UTF-8"?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="http://www.baidu.com/schemas/sitemap-mobile/1/">')

	for link in links:
		# 如果存在域名，就置空
		newlink = link.replace(url,'')
		file.write("\n\t<url>\n\t\t<loc>{0}{1}</loc>".format(url, newlink))

		file.write(mobiletypeval+"</url>")

	file.write('\n</urlset>')


